<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">订单ID：</label>
                        <div class="layui-input-inline mr0">
                            <input name="orderId" class="layui-input" type="text" placeholder="请输入订单ID"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchLog" lay-submit><i class="layui-icon">&#xe615;</i>搜索</button>
                        <button id="orderProBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                        <button id="orderProBtnExport" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="orderProTable" lay-filter="orderProTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="orderProTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="orderProForm">
    <form lay-filter="orderProForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">创建者</label>
            <div class="layui-input-block">
                <input name="creator" placeholder="请输入创建者" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">修改者</label>
            <div class="layui-input-block">
                <input name="modifier" placeholder="请输入修改者" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">创建人</label>
            <div class="layui-input-block">
                <input name="creatorName" placeholder="请输入创建人" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">修改人</label>
            <div class="layui-input-block">
                <input name="modifierName" placeholder="请输入修改人" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">订单ID</label>
            <div class="layui-input-block">
                <input name="orderId" placeholder="请输入订单ID" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">物料类型ID</label>
            <div class="layui-input-block">
                <input name="proTypeId" placeholder="请输入物料类型ID" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">客户sku料号</label>
            <div class="layui-input-block">
                <input name="customerSkuItem" placeholder="请输入客户sku料号" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">客户物料名称</label>
            <div class="layui-input-block">
                <input name="curtomerSkuName" placeholder="请输入客户物料名称" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">客户规格码</label>
            <div class="layui-input-block">
                <input name="curtomerSpecCode" placeholder="请输入客户规格码" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">物料单位ID</label>
            <div class="layui-input-block">
                <input name="proUnitId" placeholder="请输入物料单位ID" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否需要包装</label>
            <div class="layui-input-block">
                <input name="whetherPack" placeholder="请输入是否需要包装" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">包装说明</label>
            <div class="layui-input-block">
                <input name="packExplain" placeholder="请输入包装说明" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">需求数量</label>
            <div class="layui-input-block">
                <input name="quantity" placeholder="请输入需求数量" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <select name="status">
                        <option value="pending">待受理</option>
                        <option value="accepted">已受理</option>
                        <option value="refuse">不受理</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">物料ID,无论新建还是关联内部,prodetail表ID</label>
            <div class="layui-input-block">
                <input name="relationProId" placeholder="请输入物料ID,无论新建还是关联内部,prodetail表ID" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">建议最低单价</label>
            <div class="layui-input-block">
                <input name="suggestLowestPrice" placeholder="请输入建议最低单价" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">建议单价</label>
            <div class="layui-input-block">
                <input name="suggestPrice" placeholder="请输入建议单价" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">优惠单价</label>
            <div class="layui-input-block">
                <input name="discountPrice" placeholder="请输入优惠单价" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">终审结果</label>
            <div class="layui-input-block">
                <select name="finalResult">
                        <option value="refuse">不接单</option>
                        <option value="pass">接单</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="orderProFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#orderProTable',
            url: config.base + 'enterprise/sell/admin/orderPro/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'}
                ,{field: 'creatorName', sort: true, title: '创建人'}
                ,{field: 'modifierName', sort: true, title: '修改人'}
                ,{field: 'orderId', sort: true, title: '订单ID'}
                ,{field: 'proTypeId', sort: true, title: '物料类型ID'}
                ,{field: 'customerSkuItem', sort: true, title: '客户sku料号'}
                ,{field: 'curtomerSkuName', sort: true, title: '客户物料名称'}
                ,{field: 'curtomerSpecCode', sort: true, title: '客户规格码'}
                ,{field: 'proUnitId', sort: true, title: '物料单位ID'}
                ,{field: 'whetherPack', sort: true, title: '是否需要包装'}
                ,{field: 'packExplain', sort: true, title: '包装说明'}
                ,{field: 'quantity', sort: true, title: '需求数量'}
                ,{field: 'statusDesc', sort: true, title: '状态'}
                ,{field: 'relationProId', sort: true, title: '物料ID,无论新建还是关联内部,prodetail表ID'}
                ,{field: 'suggestLowestPrice', sort: true, title: '建议最低单价'}
                ,{field: 'suggestPrice', sort: true, title: '建议单价'}
                ,{field: 'discountPrice', sort: true, title: '优惠单价'}
                ,{field: 'finalResultDesc', sort: true, title: '终审结果'}
                ,{field: 'createTime', sort: true, title: '创建时间'}
                ,{align: 'center', toolbar: '#orderProTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if(menuButton.indexOf("搜索") == -1){
                    $('.layui-form.toolbar').remove();
                }
                if(menuButton.indexOf("修改") == -1){
                    $("[lay-event='edit']").remove();
                }
                if(menuButton.indexOf("删除") == -1){
                    $("[lay-event='del']").remove();
                }
            }
        });

        // 工具条点击事件
        table.on('tool(orderProTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(orderProTable)', function(obj){
            table.reload('orderProTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 添加按钮点击事件
        $('#orderProBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '880px',
                offset: '65px',
                title: data ? '修改销售订单物料表' : '添加销售订单物料表',
                content: $('#orderProForm').html(),
                success: function () {
                    if (data){
                        var option = $("form select[name='status']").find("option");
                        layui.each(option, function(index, item){
                            if(data.status  == item.text){
                                data.status = item.value;
                            }
                        });
                    }

                    if (data){
                        var option = $("form select[name='finalResult']").find("option");
                        layui.each(option, function(index, item){
                            if(data.finalResult  == item.text){
                                data.finalResult = item.value;
                            }
                        });
                    }

                    form.val('orderProForm', data);
                    // 表单提交事件
                    form.on('submit(orderProFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req(data ? 'enterprise/sell/admin/orderPro/update' : 'enterprise/sell/admin/orderPro/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('orderProTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/sell/admin/orderPro/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#orderProBtnExport').click(function () {
            var checkRows = table.checkStatus('orderProTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>